<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockArticleController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/17  16:59
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;


use App\Services\StockArticleService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class StockArticleController extends Controller
{
    protected $stockArticleService;

    public function __construct(StockArticleService $stockArticleService)
    {
        $this->stockArticleService = $stockArticleService;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/18  14:14
     */
    public function index(Request $request)
    {
        $rules      = [
            'page'     => ['nullable', 'numeric', 'gte:0'],
            'limit'    => ['nullable', 'numeric', 'gte:1', 'lte:100'],
            'sort'     => ['nullable',],
            'order'    => ['nullable', 'string'],
            'title'    => ['nullable', 'string'],
            'category' => ['nullable', 'string'],
            'isOpen'   => ['nullable', 'integer', Rule::in(0, 1)],
            'start_at' => ['required_with:end_at', 'date_format:Y-m-d H:i:s', 'before_or_equal:end_at'],
            'end_at'   => ['required_with:start_at', 'date_format:Y-m-d H:i:s', 'after_or_equal:start_at'],
        ];
        $attributes = [
            'page'     => '页数',
            'limit'    => '每页条数',
            'sort'     => '排序字段',
            'order'    => '排序顺序',
            'title'    => '标题',
            'category' => '文章栏目',
            'isOpen'   => '是否公开',
            'start_at' => '开始时间',
            'end_at'   => '结束时间',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $data = $this->stockArticleService->getList($validated);

        return $this->responseSuccessData('请求成功！', $data);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/19  17:50
     */
    public function detail(Request $request)
    {
        $rules      = [
            'id' => ['required', 'numeric', 'gte:0'],
        ];
        $attributes = [
            'id' => '文章id',
        ];
        $messages   = [];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $data = $this->stockArticleService->getInfo($validated);

        return $this->responseSuccessData('请求成功！', $data);
    }

}
